package com.haiwen.cookie;

import jdk.nashorn.internal.ir.CallNode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 韩要雷
 * Cookie的获取：
 * 获取到的是所有的cookie，返回的是一个数组，获取方式用getCookie()
 * 通过getName()以及getValue() 获取里面的k-v信息
 */
@WebServlet("/cookie02")
public class cookie02 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取cookie数组
        Cookie[] cookies = req.getCookies();

        //判断
        if (cookies != null && cookies.length>0) {
            /*for (int i=0;i< cookies.length;i++){
                System.out.println(cookies[i].getName());
            }*/
            for (Cookie cookie : cookies) {
                System.out.println("name:" + cookie.getName() + " ,value" + cookie.getValue());
                //取单个键值信息
                if ("userName".equals(cookie.getName())) {
                    System.out.println("当前cookie的值为：" + cookie.getValue()) ;
                }
            }
        }
    }
}
